{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python基础-02 字符串\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串是一种表示文本数据的类型。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我爱中国\n",
      "我爱中国\n",
      "我爱中国\n",
      "我爱\"中国\"\n",
      "我爱'中国'\n",
      "\"我\"'爱'中国\n"
     ]
    }
   ],
   "source": [
    "a = '我爱中国'\n",
    "b = \"我爱中国\"\n",
    "c = '''我爱中国'''\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "# \"\"可以包含''\n",
    "# ''可以包含\"\"\n",
    "\n",
    "a = '我爱\"中国\"'\n",
    "b = \"我爱'中国'\"\n",
    "c = '''\"我\"'爱'中国'''\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 转义字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world \n",
      "hello world\n",
      "hello world\n",
      "\"hello world \thello world\thello world\"\n"
     ]
    }
   ],
   "source": [
    "# 转义字符  常用的换行\\n, 制表符\\t\n",
    "mystr ='hello world \\nhello world\\nhello world'  \n",
    "print(mystr)\n",
    "mystr ='\"hello world \\thello world\\thello world\"'\n",
    "print(mystr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "姓名：xiaoming\n",
      "职位：讲师\n",
      "公司地址：北京市昌平区建材城西路金燕龙办公楼1层\n",
      "年龄：33\n",
      "收入：1000.000000\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 字符串输出  %s 字符串  %d 整数  %f 浮点数\n",
    "name = 'xiaoming'\n",
    "position = '讲师'\n",
    "address = '北京市昌平区建材城西路金燕龙办公楼1层'\n",
    "age = 33\n",
    "salary = 1000.0\n",
    "print('--------------------------------------------------')\n",
    "print(\"姓名：%s\" % name)\n",
    "print(\"职位：%s\" % position)\n",
    "print(\"公司地址：%s\" % address)\n",
    "print(\"年龄：%d\" % age)\n",
    "print(\"收入：%f\" % salary)\n",
    "print('--------------------------------------------------')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入用户名:fanxiaowei\n",
      "用户名为：fanxiaowei\n",
      "请输入密码:password\n",
      "密码为：password\n"
     ]
    }
   ],
   "source": [
    "# 字符串输入\n",
    "userName = input('请输入用户名:')\n",
    "print(\"用户名为：%s\" % userName)\n",
    "password = input('请输入密码:')\n",
    "print(\"密码为：%s\" % password)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串切片截取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "abc\n",
      "de\n",
      "bcdefghijklm\n",
      "cdefghijklmn\n",
      "nmlkjihgfedcba\n"
     ]
    }
   ],
   "source": [
    "# 字符串切片截取[begin:end]  前闭 后开\n",
    "name=\"abcdefghijklmn\"\n",
    "print(name[0])\n",
    "print(name[0:3])  # 取下标0~2 的字符\n",
    "print(name[3:5])  # 取下标为3、4 的字符\n",
    "print(name[1:-1]) # 取下标为1开始 到 最后第2个之间的字符\n",
    "print(name[2:])    # 取下标为2开始到最后的字符\n",
    "print(name[::-1]) # 倒序从后往前，取步长为2的字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串内建函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### find函数：检测字符串是否包括子字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# find函数：检测字符串是否包括子字符串 str.find(sub, beg=0, end=len(string))\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "index = mystr.find(\"itheima\",0,30)  # 输出子串对的位置\n",
    "print(index)\n",
    "\n",
    "index = mystr.find(\"itheima\",0,10)  # 如果找不到会输出-1，不错报错\n",
    "print(index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### index函数：检测字符串是否包括子字符串 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-b4fea5fa0c74>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmystr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"itheima\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 如果找不到就会返回错误\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "# index函数：检测字符串是否包括子字符串 str.index(sub, beg=0, end=len(string))\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "index=mystr.index(\"itheima\",0,30)\n",
    "print(index)\n",
    "\n",
    "index=mystr.index(\"itheima\",0,10)  # 如果找不到就会返回错误\n",
    "print(index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### count函数：统计字符串中某个字符的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# count函数：统计字符串中某个字符的个数 str.count(sub, start= 0, end=len(string))\n",
    "\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "result=mystr.count(\"itheima\")\n",
    "print(result)\n",
    "\n",
    "result=mystr.count(\"itheima\",0,30)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### replace函数：将旧字符串替换为新字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world ITHEIMA and ITHEIMAApp\n",
      "hello world ITHEIMA and itheimaApp\n"
     ]
    }
   ],
   "source": [
    "# replace函数：将旧字符串替换为新字符串 str.replace(old, new[, max])\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "newStr=mystr.replace(\"itheima\",\"ITHEIMA\")\n",
    "print(newStr)\n",
    "\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "newStr=mystr.replace(\"itheima\",\"ITHEIMA\", 1)  # 1设置最大只替换1次\n",
    "print(newStr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### split函数：通过指定分隔符对字符串进行切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['this', 'is', 'string', 'example....wow!!!']\n",
      "['th', 's ', 's string example....wow!!!']\n",
      "['this is string example....', 'o', '!!!']\n"
     ]
    }
   ],
   "source": [
    "# str.split(sep=\"\", num=string.count(sep))  num -- 分割次\n",
    "mystr = \"this is string example....wow!!!\"\n",
    "print (mystr.split(' '))\n",
    "print (mystr.split('i',2))  # 只分割1次\n",
    "print (mystr.split('w'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### capitalize：第一个字符大写，其他字符小写\n",
    "### title：所有单词首字母大写，其余字母小写\n",
    "### upper：将小写字母转为大写字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world itheima and itheimaapp\n",
      "Hello World Itheima And Itheimaapp\n",
      "HELLO WORLD ITHEIMA AND ITHEIMAAPP\n"
     ]
    }
   ],
   "source": [
    "mystr ='hello world itheima and itheimaApp'\n",
    "newStr=mystr.capitalize()\n",
    "print(newStr)\n",
    "\n",
    "newStr=mystr.title()\n",
    "print(newStr)\n",
    "\n",
    "newStr= mystr.upper()\n",
    "print(newStr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### startswith：检查字符串是否以指定子串开头\n",
    "### endswith：检查字符串是否以指定子串结尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# str.startswith(prefix, beg=0,end=len(string))\n",
    "\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "newStr=mystr.startswith(\"hello\")\n",
    "print(newStr)\n",
    "\n",
    "# str.endswith(suffix[, start[, end]])\n",
    "newStr1=mystr.endswith(\"app\")\n",
    "newStr2=mystr.endswith(\"App\")\n",
    "print(newStr1)\n",
    "print(newStr2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ljust：左对齐，使用空格填充至指定长度的新字符串\n",
    "### rjust：右对齐，使用空格填充至指定长度的新字符串\n",
    "### center：返回一个指定的宽度 width 居中的字符串 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world itheima and itheimaApp****************\n",
      "________________hello world itheima and itheimaApp\n",
      "        hello world itheima and itheimaApp        \n"
     ]
    }
   ],
   "source": [
    "# str.ljust(width[, fillchar]) width -- 指定字符串长度。fillchar -- 填充字符，默认为空格。\n",
    "mystr ='hello world itheima and itheimaApp'\n",
    "print (mystr.ljust(50, '*'))\n",
    "\n",
    "# str.rjust(width[, fillchar])\n",
    "print(mystr.rjust(50, '_'))\n",
    "\n",
    "# str.center(width[, fillchar])\n",
    "print(mystr.center(50))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### lstrip：截掉字符串左边的空格或指定字符 \n",
    "### rstrip：截掉字符串右边的空格或指定字符 \n",
    "### strip：截掉字符串左右边的空格或指定字符 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world itheima and itheimaApp   \n",
      "hello world itheima and itheimaApp*****\n",
      "*******hello world itheima and itheimaApp\n",
      "hello world itheima and itheimaApp\n",
      "hello world itheima and itheimaApp\n"
     ]
    }
   ],
   "source": [
    "mystr1='   hello world itheima and itheimaApp   '\n",
    "mystr2='*******hello world itheima and itheimaApp*****'\n",
    "\n",
    "# str.lstrip([chars]) 截左侧\n",
    "print(mystr1.lstrip())  # 默认空格\n",
    "print(mystr2.lstrip('*'))\n",
    "\n",
    "# rstrip 截右侧\n",
    "print(mystr2.rstrip('*'))\n",
    "\n",
    "# strip  截两头\n",
    "print(mystr1.strip())\n",
    "print(mystr2.strip('*'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bread and bread and \n",
      "---------------\n",
      "b\n",
      "br\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 定义两个字符串\n",
    "str1 = 'bread and '\n",
    "str2 = 'bread and '\n",
    "print(str1 + str2) # 使用+运算符连接两个字符串\n",
    "print('-'*15)       # 使用*运算符输出一条横线\n",
    "print(str1[0])      # 获取索引位置为0的字符\n",
    "print(str2[0:2])    # 截取索引0~1的字符\n",
    "print('read' in str1)  # 判断字符是否在字符串中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
